﻿// ActionScript file
/**
 * 嵌套菜单  创建多个MainButton
 */
package org.nickro.controls{
	import caurina.transitions.*;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	
	import org.nickro.geom.Rotator;
	
	public class NestedMenu extends BaseControls{
		private var menus:Array;
		private var _mainButton:Class;
		private var _subButton:Class;
		private var selectedIndex:int;
		private var _direction:String="vertical"
		//旋转系数数组
		private var rotations:Array=new Array();
		private var rotators:Array=new Array;
		//旋转点坐�?
		private var pX:Number =80;
		private var pY:Number =50;;
		//旋转间隔
		private var rotationInterval:Number = 6;
		public function NestedMenu(dataProvider:Object,mainButton:Class,subButton:Class){
			_mainButton=mainButton;
			_subButton=subButton;
			super(dataProvider);
		}
		override protected function createChildren():void{
			menus=new Array();
			for(var i:int=0;i<itemNumber;i++){
				//trace(_mainButton,_subButton);
				var mainBtn:MainButton=new MainButton(data[i],_mainButton,_subButton);
				
				mainBtn.addEventListener(MainButton.MAIN_CLICK,onDown);
				mainBtn.addEventListener(MainButton.MAIN_OUT,onOut);
				mainBtn.addEventListener(MainButton.MAIN_OVER,onOver);
				addChild(mainBtn);
				trace(mainBtn.height);
				menus.push(mainBtn);
				var rotator:Rotator=new Rotator(mainBtn,new Point(pX,pY));
				rotators.push(rotator);
			}
				
			updateDisplayObject();
			getRotations();
		}
		private function onDown(e:Event):void{
			selectedIndex=getChildIndex(DisplayObject(e.target));
			MainButton(getChildAt(selectedIndex)).mainSelected=true;
			for(var i:int=0;i<itemNumber;i++){
				if( i != selectedIndex){
					MainButton(getChildAt(i)).mainSelected=false;
				}
			}
		}
		
		private function getRotations():void{
			for(var i:int=0;i<data.length();i++){
				var rot:Number=rotators[i].rotation;
				if(rotations[i] != null){
					rotations.shift();
					rotations.push(rot);
				}else{
					rotations.push(rot);
				}
			}
		}
		
		private function onOut(e:Event):void{
			/**
			 * @移过MainButton效果
			 */
			 for(var j:int=0;j<data.length();j++){
				Tweener.addTween(rotators[j],{rotation:rotations[j],time:.3});
			}
		}
		
		private function onOver(e:Event):void{
			/**
			 * @移出MainButton效果
			 */
			selectedIndex=this.getChildIndex(DisplayObject(e.currentTarget));
			for(var i:int=0;i<data.length();i++){
				Tweener.addTween(rotators[i],{rotation:-(rotationInterval*(i-selectedIndex)),time:.3});
			}
		}
		
		public function updateDisplayObject():void{
			var totalX:Number=0;
			var totalY:Number=0;
			for(var i:int=0;i<itemNumber;i++){
			/**
			 * @更新布局
			 */
				switch(_direction){
					case "horizontal":
						for(var i:int=0;i<itemNumber;i++){
							menus[i].x = totalX;
							totalX += 2+ menus[i].width;
						}
					break;
					case "vertical":
						for(var i:int=0;i<itemNumber;i++){
							menus[i].y = totalY;
							totalY += 2 + menus[i].height;
						}
					break;
				}
			}
		}
	}
}